﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using Caliburn.Micro;
using GoToCoord.Models;
using System.Diagnostics;

namespace GoToCoord.Views
{
    public partial class MainView : PhoneApplicationPage, IHandle<AddLocationEvent>, IHandle<TurnWatcherOnOffEvent>
    {
        GeoCoordinateWatcher watcher;
        Pushpin currentPositionPushPin;
        private bool watcherIsStarted = false;

        public MainView()
        {
            InitializeComponent();

            IEventAggregator eventAggregator = IoC.Get<IEventAggregator>();
            eventAggregator.Subscribe(this);

            map.Mode = new AerialMode(true);
            map.ZoomBarVisibility = System.Windows.Visibility.Visible;

            if (watcher == null)
            {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
                watcher.MovementThreshold = 2;
                watcher.PositionChanged +=new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            }
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            Debug.WriteLine("Status: " + e.Status);
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (currentPositionPushPin == null) 
            {
               currentPositionPushPin = new Pushpin();
               currentPositionPushPin.Background = new SolidColorBrush(Colors.Blue);

               map.Children.Add(currentPositionPushPin);
            }
            GeoCoordinate g = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
            currentPositionPushPin.Location = g;
            map.Center = g;
            map.ZoomLevel = 15;
        }

        public void Handle(AddLocationEvent message)
        {
            GeoCoordinate g = new GeoCoordinate(message.Latitude, message.Longitude);
            map.Center = g;
            map.ZoomLevel = 15;

            Pushpin pin = new Pushpin();
            pin.Location = g;

            map.Children.Add(pin);
        }

        public void Handle(TurnWatcherOnOffEvent message)
        {
            if (!watcherIsStarted)
            {
                watcher.Start();
                watcherIsStarted = true;
            }
            else
            {
                watcher.Stop();
                watcherIsStarted = false;
                map.Children.Remove(currentPositionPushPin);
                currentPositionPushPin = null;
            }
        }
    }
}